package com.wly
{
	import com.netease.protobuf.WritingBuffer;
	import com.wly.gpb.Hero;
	import com.wly.gpb.IntMessage;
	import com.wly.gpb.LogIn;
	import com.wly.gpb.Player;
	import com.wly.gpb.StringMessage;
	
	import flash.display.Sprite;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.Socket;
	import flash.profiler.showRedrawRegions;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	
	public class ClientMain extends Sprite
	{
		static private var IPADDR:String = "127.0.0.1";
		static private var PORT:int      = 1234;
		
		private var _socket:Socket;
		private var _byteArr:ByteArray;
		
		private var _tfMessage:TextField;
		
		public function ClientMain()
		{
			super();
			_tfMessage = getChildByName("tfMesage") as TextField;
			initSocket();
			//sendPerson();
			sendLogin();
		}
		
		private function initSocket():void
		{
			_socket = new Socket();
			_socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
			_socket.addEventListener(ProgressEvent.SOCKET_DATA, onData);
			_socket.connect( IPADDR, PORT);
		}
		
		private function onError(e:IOErrorEvent):void
		{
			trace(this,"socket err", e.text);
		}
		
		private function onData(e:ProgressEvent):void
		{
			trace(this,"getData");
			var byteArr:ByteArray = new ByteArray;
			_socket.readBytes(byteArr);
			resolveByteArr(byteArr);
		}
		
		private function resolveByteArr(byteArr:ByteArray):void
		{
			var type:int;
			var len:int;
			//byteArr.position = 2;
			len  = byteArr.readShort();
			type = byteArr.readShort();
			trace(this, "type",len, type, byteArr.length, byteArr.bytesAvailable);
			if(type == 2)
			{
				var login:LogIn = new LogIn();
				login.mergeFrom(byteArr);
				trace(this, login.password, login.userName);
				_tfMessage.appendText(login.password+"|"+login.userName+"\n");
			}
			else if(type == 3)
			{
				var intMessage:IntMessage = new IntMessage;
				intMessage.mergeFrom(byteArr);
				trace(this, intMessage.ret, intMessage.type);
				_tfMessage.appendText(intMessage.type.toString()+"|"+intMessage.ret.toString()+"\n");
			}
			else if(type == 4)
			{
				var strMsg:StringMessage = new StringMessage;
				strMsg.mergeFrom(byteArr);
				_tfMessage.appendText(strMsg.type.toString()+"|"+strMsg.msg+"\n");
			}
			else if(type == 7)
			{
				var player:Player = new Player;
				var i:int;
				player.mergeFrom(byteArr);
				_tfMessage.appendText("getPlayer name:"+player.name+" id:"+player.id+"\n");
				var hero:Hero;
				
				for(i=0; i<player.heros.length;i++)
				{
					hero = player.heros[i] as Hero;
					if(hero)
					{
						_tfMessage.appendText("getHero name:"+hero.name+" id:"+hero.id+"\n");
					}
				}
				
			}
			else
			{
				_tfMessage.appendText("unknow pakcet type："+type.toString()+"\n");
			}
		}
		
		private function sendLogin():void
		{
			var login:LogIn = new LogIn;
			login.userName = "wly";
			login.password = "123456";
			
			var byteArrTmp:ByteArray = new ByteArray;
			var byteArr:ByteArray = new ByteArray;
			login.writeTo(byteArrTmp);
			byteArrTmp.position = 0;
			byteArr.writeShort(byteArrTmp.bytesAvailable);
			byteArr.writeShort(2);
			byteArr.writeBytes(byteArrTmp);
			byteArr.position = 0;
			_socket.writeBytes(byteArr);
			
			trace(this, "send logIn", login.userName, login.password);
		}
		
		private function getByteArrayHexString(byteArr:ByteArray):String
		{
			var ret:String="";;
			
			if(!byteArr)
			{
				return "null byteArr";
			}
			
			byteArr.position = 0;
			while(byteArr.bytesAvailable)
			{
				ret += byteArr.readUnsignedByte().toString(16).toUpperCase();
			}
			return ret;
		}
	}
}